// import store from './SessionStorageStore';
(function () {
    window.store = {
        fetch : function (STORAGE_KEY) {
          return window.JSON.parse(window.sessionStorage.getItem(STORAGE_KEY) || null)
        },
        save:function (STORAGE_KEY,items) {
          let data = window.JSON.stringify(items);
          try {
              window.sessionStorage.setItem(STORAGE_KEY,data);
          } catch(e) {
              alert('请关闭浏览器的无痕模式！');
          }
          window.sessionStorage.setItem(STORAGE_KEY,data);
        },
        clear: function (STORAGE_KEY) {
          window.sessionStorage.removeItem(STORAGE_KEY);
        }
    }
    let key = 'userLoginInfo';
    let data = store.fetch(key);
	let len = window.location.pathname.split('/').length;
    if(window.location.pathname.split('/')[len-1] == 'login.html') {
        if(data) {
            window.location.href = './index.html';
            return false;
        }else {
            return false;
        }
    };
    if(!data) {
        window.location.href = './login.html';
        return false;
    };
     //安全检测
    var bConnect=0;
    function load()
    {
        if(navigator.userAgent.indexOf("MSIE")>0 && !navigator.userAgent.indexOf("opera") > -1) 
        {
            return;
        }
        try
        {
            var s_pnp=new SoftKey6W();
            s_pnp.Socket_UK.onopen = function() 
            {
                bConnect=1;
            } 
            
            s_pnp.Socket_UK.onmessage =function got_packet(Msg) 
            {
                var PnpData = JSON.parse(Msg.data);
                if(PnpData.type=="PnpEvent")
                {
                    if(PnpData.IsIn) 
                    {
                        
                    }
                    else
                    {
                        // _vm.tips = '警告！未检测到安全密钥，请插入安全密钥！如已插入密钥请尝试重新拔插！';
                        // if(_vm.times === 0) {
                            store.clear('userLoginInfo');
                            store.clear('userData');
                            window.location.href = './login.html'
                        // }
                        
                    }
                }
            } 
            
            s_pnp.Socket_UK.onclose = function()
            {
                
            }
        
        
    }
    catch(e)  
    {  
            alert(e.name + ": " + e.message);
            return false;
        }  
    
    }
    load()

    // 分页处理
    window.pageSet = () => {
        delLi = (k,j) => {
            let pages = $('.pagination li');
            for (let index = k; index < j; index++) {
                $(pages[index]).remove();
            }
            let dom = `<li>
                        <a href="javascript:void(0);" class="u_page_n">
                            <span aria-hidden="true">...</span>
                        </a>
                    </li>`
            $(pages[k-1]).after(dom)
        };
        setTimeout(() => {
            let pages = $('.pagination li');
            let num = 0;
            for (const item of pages) {
                if(item.getAttribute('class') == 'active') {
                    if(num > 6) {
                        delLi(2,num - 2)
                    }
                }else {
                    num++;
                }
            }
        }, 10);
    };

    //第一次登录修改密码
    function resetPassword() {
        let key = 'userData';
        let data = store.fetch(key);
        if(window.location.pathname == '/setAccount.html') {
            return false;
        }
        if(data.status === 1) {
            alert('第一次登录请重新设置密码，否则无法进入系统！');
            window.location.href = './setAccount.html'
        }
    };

    resetPassword();
})()